package capstoneproject.dudiachi.Main;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import capstoneproject.dudiachi.Model.Festival;

public class FestivalGridCellAdapter extends BaseAdapter implements
		OnClickListener {

	private Context context;
	private ArrayList<Festival> list;
	private ArrayList<Bitmap> bitmap;
	private GridItems[] items;

	public FestivalGridCellAdapter(Context context,
			ArrayList<Festival> festivalEventList, ArrayList<Bitmap> bitArray,
			GridItems[] gridItems) {
		super();
		this.context = context;
		this.list = festivalEventList;
		this.bitmap = bitArray;
		this.items = gridItems;
	}

	public GridItems[] getItems() {
		return items;
	}

	public class ViewHolder {
		public ImageView image;
		public TextView name;

	}

	public void setItems(GridItems[] items) {
		this.items = items;
	}

	@Override
	public int getCount() {
		return items.length;
	}

	@Override
	public Object getItem(int pos) {
		if (items != null && pos >= 0 && pos < getCount()) {
			return items[pos];
		}
		return null;
	}

	@Override
	public long getItemId(int pos) {
		if(items != null && pos >= 0 && pos < getCount()) {
			return items[pos].id;
		}
		return 0;
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		ViewHolder viewHolder;
		if(v == null){
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = inflater.inflate(R.layout.grid_cell, parent, false);
		}
		viewHolder = new ViewHolder();
		viewHolder.image = (ImageView) v.findViewById(R.id.image);
		viewHolder.name = (TextView) v.findViewById(R.id.name);

		RelativeLayout layout = (RelativeLayout) v.findViewById(R.id.layout);
		layout.setTag("" + list.get(position).getFestivalID());
		layout.setOnClickListener(this);
		GridItems gridItems = items[position];
		setImage(position, viewHolder, gridItems.itemID);
		return v;
	}
	private void setImage(int pos, ViewHolder viewHolder, int itemID){
		viewHolder.image.setImageBitmap(bitmap.get(pos));
		viewHolder.image.setOnClickListener(this);
		viewHolder.image.setTag("" + list.get(itemID).getFestivalID());
		viewHolder.name.setText(list.get(itemID).getFestivalName());
	}
	@Override
	public void onClick(View v) {
		String tag = (String) v.getTag();

		Intent intent = new Intent(context, DetailFestivalActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		Bundle bundle = new Bundle();
		bundle.putSerializable("list", list);
		bundle.putString("tag", "lehoi");
		bundle.putString("id", tag);
		intent.putExtras(bundle);
		context.startActivity(intent);
	}

}
